<?php
/**
 * 分页类
 * 
 * @copyright  vincent
 * @license    Mediad License
 */
class Mediad2_Utility_Pagination {
	/**
	 * @var 起始页
	 */
	protected $pageIndex;
	
	/**
	 * @var 分页数
	 */
	protected $pageNum;
	
	/**
	 * @var 总记录数
	 */
	protected $pageItems;
	
	/**
	 * @var 总页数
	 */
	protected $pages;
	/**
	 * @var 当前页 
	 */
	protected $pageCurrent;
	
	/**
	 * @return the $pageIndex
	 */
	public function getPageIndex() {
		return $this->pageIndex;
	}
	
	/**
	 * @return the $pageNum
	 */
	public function getPageNum() {
		return $this->pageNum;
	}
	
	/**
	 * @return the $pageItems
	 */
	public function getPageItems() {
		return $this->pageItems;
	}
	
	/**
	 * @return the $pages
	 */
	public function getPages() {
		return $this->pages;
	}
	
	/**
	 * @return the $pageCurrent
	 */
	public function getPageCurrent() {
		return $this->pageCurrent;
	}
	
	/**
	 * @param 起始页 $pageIndex
	 */
	public function setPageIndex($pageIndex) {
		$this->pageIndex = $pageIndex;
	}
	
	/**
	 * @param 分页数 $pageNum
	 */
	public function setPageNum($pageNum) {
		$this->pageNum = $pageNum;
	}
	
	/**
	 * @param 总记录数 $pageItems
	 */
	public function setPageItems($pageItems) {
		$this->pageItems = $pageItems;
	}
	
	/**
	 * @param 总页数 $pages
	 */
	public function setPages($pages) {
		$this->pages = $pages;
	}
	
	/**
	 * @param 当前页 $pageCurrent
	 */
	public function setPageCurrent($pageCurrent) {
		$this->pageCurrent = $pageCurrent;
	}
	
	/**
	 * @param int $pageIndex 起始页  默认值：1
	 * @param int $pageNum 分页数 默认值：20
	 * @param int $pageItems 总记录数
	 * @param int $pages 总页数
	 */
	public function __construct($pageIndex = 1, $pageNum = 20, $pageItems, $pages) {
		$this->setPageIndex ( $pageIndex );
		$this->setPageNum ( $pageNum );
		$this->setPageItems ( $pageItems );
		$this->setPages ( $pages );
		$this->setPageCurrent ( $pageIndex ); //默认当前页为1
	}
	
	/**
	 * 设置页面
	 * @param postForm $actionForm 传入需要分页的form
	 * @return string|NULL
	 */
	public function setPage($actionForm) {		
		if (!empty($actionForm)) {
			
			$pageAction = null;
			
			if (! empty ( $actionForm ['first'] )) {
				$pageAction = 'first';
			}
			if (! empty ( $actionForm ['previous'] )) {
				$pageAction = 'previous';
			}
			if (! empty ( $actionForm ['next'] )) {
				$pageAction = 'next';
			}
			if (! empty ( $actionForm ['last'] )) {
				$pageAction = 'last';
			}
			
			switch ($pageAction)
			{
				case 'first':
					if($this->pageItems<=$this->pageNum)
						$pageAction='limit';
					$this->pageCurrent=$this->pageIndex;
					break;
				case 'previous':
					if($this->pageCurrent>1)
					{
						$this->pageCurrent=$this->pageCurrent-1;
						$pageAction='previous';
					}
					if($this->pageCurrent==1)
						$pageAction='first';
					break;
				case 'next':
					if($this->pageCurrent<$this->pages)
					{						
						$this->pageCurrent=$this->pageCurrent+1;
						$pageAction='next';
					}
					if($this->pageCurrent==$this->pages)
						$pageAction='last';
					break;
				case 'last':
					$this->pageCurrent=$this->pages;					
					$pageAction='last';					
					break;
			}			
			return $pageAction;			
		}else {
			return null;
		}
	}
	
	public function getPageStyle() 
	{
		if($this->pageItems<=$this->pageNum) {
			return 'limit';
		}
		
		if($this->pageCurrent==1) {
			return "first";
		}

		if($this->pageCurrent==$this->pages) {
			return "last";
		}
		
		if($this->pageCurrent > 1 && $this->pageCurrent < $this->pages)
		{						
			return 'next';
		}
				
	}
}

